package eMdP.inputHandler;

import java.util.HashMap;

import eMdP.gestors.GestorConstants;
import eMdP.inputHandler.accions.EMdPHandler.*;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.ThirdPersonHandler;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.input.joystick.Joystick;
import com.jme.input.joystick.JoystickInput;
import com.jme.renderer.Camera;
import com.jme.scene.Spatial;
import com.jmex.physics.PhysicsSpace;

import eMdP.classesBasiques.Personatge;

public class EMdPHandler extends ThirdPersonHandler {
	
	private KeyInputAction actionDisparar;
	private KeyInputAction actionPausar;
	
	public static final String PROP_KEY_DISPARAR = "dispararAction";
	public static final String PROP_KEY_F10 = "menuAction";
	// Variable que ens indicara quin control d'entrada utilitzarem. Contindrà un Enter, de 0 a 3.
	public static final String PROP_CONTROL_ENTRADA = "controlEntrada";
	
	private Personatge personatge;
	
	public int controlEntrada;
	
	private JoystickInput input;
	private int numJoystick;
	private Joystick test;
	
	public PhysicsSpace espaiFisic;
	
	
	public EMdPHandler(Personatge target, Camera cam, HashMap<String, Object> props, PhysicsSpace espaiFisic) {
		super(target,cam,props);
        this.targetSpatial = target;
        this.camera = cam;
        this.personatge = target;
        this.espaiFisic = espaiFisic;
        this.input = JoystickInput.get();
        this.controlEntrada = getIntProp(props, PROP_CONTROL_ENTRADA, 0);
        
        inicialitzarAccions();
        
       
        switch (controlEntrada) {
		case 0:
			props.put(PROP_KEY_FORWARD, GestorConstants.PROP_ENDEVANT_TECLAT1);
			props.put(PROP_KEY_BACKWARD, GestorConstants.PROP_ENDARRERA_TECLAT1);
			props.put(PROP_KEY_RIGHT, GestorConstants.PROP_DRETA_TECLAT1);
			props.put(PROP_KEY_LEFT, GestorConstants.PROP_ESQUERRA_TECLAT1);
			
			props.put(PROP_KEY_DISPARAR, GestorConstants.PROP_DISPARAR_TECLAT1);
			
			inicialitzarTecles(props);
			break;
		case 1:
			props.put(PROP_KEY_FORWARD, GestorConstants.PROP_ENDEVANT_TECLAT2);
			props.put(PROP_KEY_BACKWARD, GestorConstants.PROP_ENDARRERA_TECLAT2);
			props.put(PROP_KEY_RIGHT, GestorConstants.PROP_DRETA_TECLAT2);
			props.put(PROP_KEY_LEFT, GestorConstants.PROP_ESQUERRA_TECLAT2);
			
			props.put(PROP_KEY_DISPARAR, GestorConstants.PROP_DISPARAR_TECLAT2);
			
			inicialitzarTecles(props);
			break;
		case 2:
			System.out.println("Joystick configurat: "+this.test);
	        this.numJoystick = 0;
	        this.test = input.getJoystick(numJoystick);
			
			break;
		case 3:
	        this.numJoystick = 1;
	        this.test = input.getJoystick(numJoystick);
			
			break;

		default:
			props.put(PROP_KEY_FORWARD, GestorConstants.PROP_ENDEVANT_TECLAT1);
			props.put(PROP_KEY_BACKWARD, GestorConstants.PROP_ENDARRERA_TECLAT1);
			props.put(PROP_KEY_RIGHT, GestorConstants.PROP_DRETA_TECLAT1);
			props.put(PROP_KEY_LEFT, GestorConstants.PROP_ESQUERRA_TECLAT1);
		
			props.put(PROP_KEY_DISPARAR, GestorConstants.PROP_DISPARAR_TECLAT1);
			break;
		}
        
      
        
      /*  KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
        keyboard.set(PROP_KEY_DISPARAR, getIntProp(props, PROP_KEY_DISPARAR, KeyInput.KEY_X));
        keyboard.set(PROP_KEY_F10, getIntProp(props, PROP_KEY_F10, KeyInput.KEY_F10));
        */
        
        super.setEnabled(true);
        
    }
	
	protected void inicialitzarTecles(HashMap<String, Object> props){
		
		System.out.println("Inicialitzem les tecles");
		KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
		
		System.out.println("ddd "+PROP_KEY_FORWARD+""+this.controlEntrada);
		keyboard.set(PROP_KEY_FORWARD+""+this.controlEntrada, getIntProp(props, PROP_KEY_FORWARD, KeyInput.KEY_U));
		keyboard.set(PROP_KEY_BACKWARD+""+this.controlEntrada, getIntProp(props, PROP_KEY_BACKWARD, KeyInput.KEY_J));
		keyboard.set(PROP_KEY_RIGHT+""+this.controlEntrada, getIntProp(props, PROP_KEY_RIGHT, KeyInput.KEY_K));
		keyboard.set(PROP_KEY_LEFT+""+this.controlEntrada, getIntProp(props, PROP_KEY_LEFT, KeyInput.KEY_H));
		
        keyboard.set(PROP_KEY_DISPARAR+""+this.controlEntrada, getIntProp(props, PROP_KEY_DISPARAR, KeyInput.KEY_X));
        
        keyboard.set(PROP_KEY_F10, getIntProp(props, PROP_KEY_F10, KeyInput.KEY_F10));
        super.updateProperties(props);
	}
	
	protected void inicialitzarAccions() {
		System.out.println("Inicialitzem les accions");
		
		actionForward = new EndavantEMdPAction(this,10f);
		actionBack = new EndarreraEMdPAction(this,10f);
		actionRight = new DretaEMdPAction(this,10f);
		actionLeft = new EsquerraEMdPAction(this,10f);
				
        actionDisparar = new DispararEMdPHandler( personatge, this, espaiFisic );
        actionPausar =  new PausarEMdPAction();
     
        //Eliminem totes les accions del Pare, ja que sino tindrem 2 accions per anar endevant,darrera,...
        super.removeAllActions();
        
        assignarAccions();
	}
	
	protected void assignarAccions(){

		System.out.println("asignarAccions: "+PROP_KEY_FORWARD+""+this.controlEntrada);
		addAction(actionForward, PROP_KEY_FORWARD+""+this.controlEntrada,true);
		addAction(actionBack, PROP_KEY_BACKWARD+""+this.controlEntrada,true);
		addAction(actionRight, PROP_KEY_RIGHT+""+this.controlEntrada,true);
		addAction(actionLeft, PROP_KEY_LEFT+""+this.controlEntrada,true);
		
        addAction(actionDisparar, PROP_KEY_DISPARAR+""+this.controlEntrada, false);
        
        addAction(actionPausar, PROP_KEY_F10, false);
        
        //addAction( actionDisparar, InputHandler.DEVICE_ALL, 1, InputHandler.AXIS_NONE, false );
    }
	
	public Spatial getJugador(){
		return  this.targetSpatial;
	}
	
	public void update(float tpf){
		
		//Actualitzem la força que apliquem al personatge perquè es mantingui en peus
		personatge.update(tpf);
		//Actualitzem l'inputHandler pare, que es el thirdPersonHAndler, per tal que ens giri el personatge
		super.update(tpf);
		//Activem l'animació d'esperar si no està moguent-se
		if(!this.nowTurning && !this.walkingForward && !this.walkingBackwards ){
			if(!personatge.estaTirant()){
				personatge.posaEsperant();
			}
		}
	}	
	
	@Override
	protected void updateFromJoystick(float time) {
		// Si controlEntrada > 1, significa que controloem amb un joysTick
		if(controlEntrada > 1){
			input.update();
			InputActionEvent evt = new InputActionEvent();
	        for (int i = 0; i < test.getAxisCount(); i++) {
	            float val = test.getAxisValue(i);
	            if (val != 0){
	           	 if(test.getAxisNames()[i]=="y"){
	           		if(val > 0){
	           			evt.setTime(time*-val);
	                    actionForward.performAction(evt);    
	           		}else if(val < 0){
	           		 evt.setTime(time*val);
	                 actionBack.performAction(evt);
	           		}
	           	 }else if(test.getAxisNames()[i]=="x"){
	           		 if(val > 0){
	           			evt.setTime(time*val);
	                    actionRight.performAction(evt);
	           		}else if(val < 0){
	           			evt.setTime(time*-val);
	           			actionLeft.performAction(evt);
	           		}
	           	 }
		        }
		        boolean pushed = test.isButtonPressed(0);
		        
		        /*try{
		       	 if(pushed) Thread.sleep(100);
		        }catch (InterruptedException e) {}*/
		        if (pushed){
		        	actionDisparar.performAction(evt);
		        }
	        }      
		}
    }
}


